import axios from 'axios';
import query from 'query-string';
import { TreeNodeData } from '@arco-design/web-vue';

const BASE_URL = '/product/category';


export interface CategoryParam {
    name?: string;
    page?: number;
    size?: number;
    sort?: Array<string>;
}

export interface CategoryResponse {
    id?: number;
    name: string;
    parentId?: number;
    level?: number;
    status?: number;
    sort?: number;
    icon?: string;
    
    createdBy?: string;
    updatedBy?: string;
    createdAt?: string;
    updatedAt?: string;
}

export interface CategorySaveCommand {
    id?: number;
    name: string;
    parentId?: number;
    level?: number;
    status?: number;
    sort?: number;
    icon?: string;
}

export interface CategoryResponseList {
    list: CategoryResponse[];
    total: number;
}

// 获取菜单树
export function treeCategory(params: CategoryParam) {
    return axios.get<TreeNodeData[]>(`${BASE_URL}/common/tree`, {
        params, paramsSerializer: (obj) => {return query.stringify(obj);}
    });
}


export function treeList(params: CategoryParam) {
    return axios.get<CategoryResponse[]>(`${BASE_URL}/tree/list`, {
        params, paramsSerializer: (obj) => {return query.stringify(obj);}
    });
}

// 通过CategoryParam参数获取分页列表
export function pageCategory(params: CategoryParam) {
    return axios.get<CategoryResponseList>(`${BASE_URL}/page`, {
        params, paramsSerializer: (obj) => {
            return query.stringify(obj);
        }
    })
}

// 通过id获取category信息
export function getCategory(id: number) {
    return axios.get<CategoryResponse>(`${BASE_URL}/${id}`);
    
}

export function listCategory(params: CategoryParam) {
    return axios.get<CategoryResponse[]>(`${BASE_URL}/list`, {
        params, paramsSerializer: (obj) => {
            return query.stringify(obj);
        }
    });
}

// 添加category
export function addCategory(params: CategorySaveCommand) {
    return axios.post(`${BASE_URL}/add`, params);
}

// 更新category
export function updateCategory(params: CategorySaveCommand) {
    return axios.put(`${BASE_URL}/update`, params);
}

// 批量删除category
export function deleteCategory(ids: number | Array<number>) {
    return axios.delete(`${BASE_URL}/delete/${ids}`);
}